package com.scenic.rd_android.threerd.rxjava_retrofit;

import android.Manifest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.jakewharton.rxbinding.view.RxView;
import com.nova.scenic.projectlibs.util.debug.MyLog;
import com.scenic.rd_android.R;
import com.tbruyelle.rxpermissions.RxPermissions;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import rx.Subscription;
import rx.functions.Action1;
import rx.subjects.PublishSubject;
import rx.subjects.SerializedSubject;
import rx.subjects.Subject;

/**
 * http://android.jobbole.com/83416/
 */
public class TestRXJava extends AppCompatActivity {

    MyLog myLog = new MyLog("TestRXJava");
    {
        myLog.setPattern("scenic_log %t%6 %c %l - %m");
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_rxjava);
        Button button = (Button) findViewById(R.id.id_button_rxJava_test1);
        RxView.clicks(button).subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                     myLog.info("button click");
            }
        });
        myLog.debug(" begin button click");
        RxView.clicks(button).debounce(300, TimeUnit.MILLISECONDS).subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                myLog.info("button click");
            }
        });
    }
    rx.Observable<Long> observable = null;
    public void testInterval(View view) {
        if(observable != null){

        }
        //TODO(liuzd) How to stop it ?
        observable = rx.Observable.interval(2, 2, TimeUnit.SECONDS);
        observable.subscribe(new Action1<Long>() {
            @Override
            public void call(Long aLong) {
                myLog.info("test interval");
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(observable != null){

        }
    }

    public void testRxPermissions(View view) {
        Subscription subscribe = RxPermissions.getInstance(this).request(Manifest.permission.CAMERA, Manifest.permission.READ_PHONE_STATE)
                .subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean aBoolean) {
                        myLog.info("boolean is " + aBoolean);
                    }
                });

    }

    public interface CreateInstanceCallback<T>{
        T onGetInstance();
    }

    Map<String,Object> mVariable = new HashMap<>();
    private <T> T getVariable(String key,CreateInstanceCallback<T> type){
        if(!mVariable.containsKey(key)){
            mVariable.put(key,type.onGetInstance());
        }
        return (T) mVariable.get(key);
    }

    public void testRxBus(View view) {
        getVariable("testRxBus", new CreateInstanceCallback<Subscription>() {
            @Override
            public Subscription onGetInstance() {
                return RxBus.getInstance().tObservable(String.class).subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        myLog.info("the param string is " + s);
                    }
                });
            }
        });


        RxBus.getInstance().post("send message to test RxBus");
    }



     static class RxBus {
        private final Subject<Object,Object> subject;

         private static class RxBusHolder {
             private static final RxBus instance = new RxBus();
         }

        private RxBus(){
            subject = new SerializedSubject<>(PublishSubject.create());
        }
        public static RxBus getInstance(){
            return RxBusHolder.instance;
        }

         public void post(Object o){
             subject.onNext(o);
         }

         public <T>rx.Observable<T> tObservable(Class<T> eventType){
             return subject.ofType(eventType);
         }
    }
}
